package com.feng.animations.views;

import com.feng.animations.R;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.view.KeyEvent;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;

public class TweensView extends View 
{	
	private Animation iAnim = null;
	private Bitmap pict = null;

	public TweensView(Context context) 
	{
		super(context);
		
		this.pict = BitmapFactory.decodeResource
			(getResources(), R.drawable.black_drop);
		
	}

	@Override
	protected void onDraw(Canvas canvas) 
	{
		super.onDraw(canvas);
		
		canvas.drawBitmap(this.pict, 0, 0, null);
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) 
	{
		switch (keyCode)
		{
			case KeyEvent.KEYCODE_DPAD_UP:
				this.iAnim = new AlphaAnimation (1.0F, 0.1F);
				this.iAnim.setDuration(3000);
				break;
			case KeyEvent.KEYCODE_DPAD_DOWN:
				this.iAnim = new ScaleAnimation (0.0F, 1.0F, 0.0F, 1.0F, 
						Animation.RELATIVE_TO_SELF, 0.5F, Animation.RELATIVE_TO_SELF, 0.5F);
				this.iAnim.setDuration(500);
				break;
			case KeyEvent.KEYCODE_DPAD_LEFT:
				this.iAnim = new TranslateAnimation (10, 100, 10, 100);
				this.iAnim.setDuration(1000);
				break;
			case KeyEvent.KEYCODE_DPAD_RIGHT:
				this.iAnim = new RotateAnimation (0.0F, 360.0F, Animation.RELATIVE_TO_SELF,
						0.5F, Animation.RELATIVE_TO_SELF, 0.5F);
				this.iAnim.setDuration (1000);
				break;
			default:
				return false;
		}
		this.startAnimation(this.iAnim);
		return true;
	}
	

}
